package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;

import deck.Card;

public class HandImage {
	private List<Card> cards;
	private Dimension dimensions;
	private boolean multiSelect;
	private String handle;
	
	public static final int STACKED_CARD_WIDTH = 15;

	public HandImage(List<Card> cards) {
		this.cards = cards;
		this.dimensions = new Dimension(0, 0);
		this.multiSelect = false;
		this.handle = "UNKNOWN";
	}
	
	public void setHandle(String handle){
		this.handle = handle;
	}
	
	public String getHandle(){
		return handle;
	}
	
	public void replaceHand(List<Card> cards){
		this.cards = cards;
	}
	
	public void setMultiSelect(boolean multiSelect){
		this.multiSelect = multiSelect;
	}
	
	public void setPlayableCards(List<Card> cards){
		for(Card card : this.cards){
			if(cards.contains(card)){
				card.setPlayable(true);
			}else{
				card.setPlayable(false);
			}
		}
	}
	
	public void setSelectedCards(List<Card> cards){
		for(Card card : this.cards){
			if(cards.contains(card)){
				card.setSelected(true);
			}else{
				card.setSelected(false);
			}
		}
	}
	
	public void setSelectedCard(Card c){
		for(Card card : this.cards){
			if(card.equals(c)){
				if(multiSelect)
					card.setSelected(!card.isSelected());
				else
					card.setSelected(true);
			}else if(!multiSelect){
				card.setSelected(false);
			}
		}
	}
	
	public Dimension getSize(){
		return dimensions;
	}
	
	public List<Card> getCards(){
		return cards;
	}
	
	public void recalculateSize(Graphics g){
		int xoffset = 0;
		Image image = null;
		for(Card card : cards){
			image = CardImages.getImage(card.getSuit(), card.getValue());
			xoffset += STACKED_CARD_WIDTH;
		}
		xoffset += image == null ? 0 : image.getWidth(null) - STACKED_CARD_WIDTH;
		this.dimensions.height = image == null ? 0 : image.getHeight(null);
		this.dimensions.width = xoffset;
	}
	
	public void draw(Graphics g){
		int width = 0;
		Image image = null;
		for(Card card : cards){
			image = CardImages.getImage(card.getSuit(), card.getValue());
			int yoffset = 0;
			if(card.isSelected())
				yoffset = -10;
			g.drawImage(image, width, yoffset, null);
			if(!card.isPlayable()){
				g.setColor(new Color(0f, 0f, 0f, .25f));
				g.fillRect(width, 0, image.getWidth(null), image.getHeight(null));
			}
			width += STACKED_CARD_WIDTH;
		}
		recalculateSize(g);
	}
}
